package com.ruoyi.controller.production;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.production.MesProcessRoute;
import com.ruoyi.service.production.IMesProcessRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 工艺路线Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/production/processRoute")
public class MesProcessRouteController extends BaseController
{
    @Autowired
    private IMesProcessRouteService IMesProcessRouteService;

    /**
     * 查询工艺路线列表
     */
    @PreAuthorize("@ss.hasPermi('production:processRoute:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesProcessRoute mesProcessRoute)
    {
        startPage();
        List<MesProcessRoute> list = IMesProcessRouteService.selectMesProcessRouteList(mesProcessRoute);
        return getDataTable(list);
    }

    /**
     * 导出工艺路线列表
     */
    @PreAuthorize("@ss.hasPermi('production:processRoute:export')")
    @Log(title = "工艺路线", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesProcessRoute mesProcessRoute)
    {
        List<MesProcessRoute> list = IMesProcessRouteService.selectMesProcessRouteList(mesProcessRoute);
        ExcelUtil<MesProcessRoute> util = new ExcelUtil<MesProcessRoute>(MesProcessRoute.class);
        util.exportExcel(response, list, "工艺路线数据");
    }

    /**
     * 获取工艺路线详细信息
     */
    @PreAuthorize("@ss.hasPermi('production:processRoute:query')")
    @GetMapping(value = "/{routeId}")
    public AjaxResult getInfo(@PathVariable("routeId") Long routeId)
    {
        return success(IMesProcessRouteService.selectMesProcessRouteByRouteId(routeId));
    }

    /**
     * 新增工艺路线
     */
    @PreAuthorize("@ss.hasPermi('production:processRoute:add')")
    @Log(title = "工艺路线", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesProcessRoute mesProcessRoute)
    {
        if ("1".equals(IMesProcessRouteService.checkRouteCodeUnique(mesProcessRoute.getRouteCode(),mesProcessRoute.getRouteId())))
        {
            return error("新增工艺路线'" + mesProcessRoute.getRouteName() + "'失败，工艺路线编码已存在");
        }
        return toAjax(IMesProcessRouteService.insertMesProcessRoute(mesProcessRoute));
    }

    /**
     * 修改工艺路线
     */
    @PreAuthorize("@ss.hasPermi('production:processRoute:edit')")
    @Log(title = "工艺路线", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesProcessRoute mesProcessRoute)
    {
        if ("1".equals(IMesProcessRouteService.checkRouteCodeUnique(mesProcessRoute.getRouteCode(),mesProcessRoute.getRouteId())))
        {
            return error("修改工艺路线'" + mesProcessRoute.getRouteName() + "'失败，工艺路线编码已存在");
        }
        return toAjax(IMesProcessRouteService.updateMesProcessRoute(mesProcessRoute));
    }

    /**
     * 删除工艺路线
     */
    @PreAuthorize("@ss.hasPermi('production:processRoute:remove')")
    @Log(title = "工艺路线", businessType = BusinessType.DELETE)
    @DeleteMapping("/{routeIds}")
    public AjaxResult remove(@PathVariable Long[] routeIds)
    {
        return toAjax(IMesProcessRouteService.deleteMesProcessRouteByRouteIds(routeIds));
    }

    /**
     * 校验工艺路线编码
     */
    @PostMapping("/checkRouteCodeUnique")
    public AjaxResult checkRouteCodeUnique(@RequestBody MesProcessRoute mesProcessRoute)
    {
        String result = IMesProcessRouteService.checkRouteCodeUnique(mesProcessRoute.getRouteCode(),mesProcessRoute.getRouteId());
        return AjaxResult.success(result.equals("0")); // 返回true表示编码可用
    }
}